package com.zysc.processflow.controller;


import com.baomidou.mybatisplus.extension.service.IService;
import com.zysc.processflow.common.base.ResultBody;
import com.zysc.processflow.entity.Organization;
import com.zysc.processflow.entity.Personorg;
import com.zysc.processflow.entity.Personrole;
import com.zysc.processflow.entity.Role;
import com.zysc.processflow.service.IHandlerecorddetailService;
import com.zysc.processflow.service.IPersonroleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import com.zysc.processflow.common.base.BaseController;

import java.util.List;

/**
 * <p>
 * 人员角色表 前端控制器
 * </p>
 *
 * @author 正元智慧城市_
 * @since 2022-11-23
 */
@Api(value = "人员和角色分配", tags = "人员和角色分配")
@RestController
@RequestMapping("/personrole")
public class PersonroleController extends BaseController<Personrole> {

    @Autowired
    IPersonroleService service;

    public PersonroleController(IPersonroleService service) {
        super(service);
    }


    @ApiOperation("获取人员和角色的关系数据，通过Id")
    @GetMapping(value = "getroleListByPersonid")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "人员id", name = "personid", dataType = "String"),
    })
    public ResultBody getroleListByPersonid(String personid) {
        List<Role> list = service.getroleListByPersonid(personid);
        return ResultBody.Success(list);
    }

    @ApiOperation("删除用户角色关系")
    @PostMapping("DeletePersonrole")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "人员角色关系实体", name = "entity", dataType = "Personrole")
    })
    public ResultBody DeletePersonrole(@RequestBody Personrole entity) {
        try{
            Boolean b = service.DeletePersonroleByPersonidAndRoleid(entity);
            if(!b){
                throw new RuntimeException("删除人员信息失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultBody.error(e.getMessage(), 500);
        }
        return ResultBody.Success(entity);
    }


    @ApiOperation("添加/更新人员-角色关系信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "人员-角色关系信息", name = "entity", dataType = "Personrole")
    })
    @Transactional
    @PostMapping(value = "SaveOrUpdate")
    public ResultBody SaveOrUpdate(@RequestBody Personrole entity) {
        try{
            Boolean b = service.saveOrUpdatePersonroleInfo(entity);
            if(!b){
                throw new RuntimeException("保存人员-单位关系信息失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultBody.error(e.getMessage(), 500);
        }
        return ResultBody.Success(entity);
    }

    @ApiOperation("添加/更新人员-角色关系信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "人员id", name = "personid", dataType = "String"),
            @ApiImplicitParam(value = "人员名称", name = "personname", dataType = "String"),
            @ApiImplicitParam(value = "角色ids", name = "roleidlist", dataType = "List"),
            @ApiImplicitParam(value = "单位id", name = "orgid", dataType = "String"),
            @ApiImplicitParam(value = "单位名称", name = "orgname", dataType = "String")
    })
    @Transactional
    @PostMapping(value = "SaveOrUpdatePersonRole")
    public ResultBody SaveOrUpdatePersonRole(@RequestParam(value = "personid",required = false)String personid,
                                             @RequestParam(value = "personname",required = false)String personname,
                                             @RequestParam(value = "roleidlist",required = false) List<String> roleidlist,
                                             @RequestParam(value = "orgid",required = false) String orgid,
                                             @RequestParam(value = "orgname",required = false) String orgname) {
        Boolean b = true;
        try{
            b = service.SaveOrUpdatePersonRole(personid,personname,orgid,orgname,roleidlist);
            if(!b){
                throw new RuntimeException("保存人员-单位关系信息失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultBody.error(e.getMessage(), 500);
        }
        return ResultBody.Success(b);
    }

}
